package seatiger.tomcat.catalina.core;

import seatiger.tomcat.catalina.Constants;
import seatiger.tomcat.catalina.HttpRequest;
import seatiger.tomcat.catalina.Wrapper;
import seatiger.tomcat.naming.resources.Resource;
import seatiger.tomcat.util.StringManager;

import javax.naming.directory.DirContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;

/**
 * ServletContext 的实现
 */
public class ApplicationContext implements ServletContext {

    private StandardContext context;

    private String basePath = null;

    private ServletContext facade = new ApplicationContextFacade(this);

    private static final StringManager sm =
            StringManager.getManager(Constants.PACKAGE_CORE);

    public ApplicationContext(String basePath, StandardContext context) {
        super();
        this.context = context;
        this.basePath = basePath;
    }


    @Override
    public ServletContext getContext(String s) {
        return null;
    }

    @Override
    public int getMajorVersion() {
        return 0;
    }

    @Override
    public int getMinorVersion() {
        return 0;
    }

    @Override
    public String getMimeType(String s) {
        return null;
    }

    @Override
    public Set getResourcePaths(String s) {
        return null;
    }

    @Override
    public URL getResource(String s) throws MalformedURLException {
        return null;
    }

    @Override
    public InputStream getResourceAsStream(String path) {
        DirContext resources = context.getResources();
        if (resources != null){
            try {
                Object resource = resources.lookup(path);
                if (resource instanceof Resource){
                    return ((Resource) resource).streamContent();
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * 通过重定向的路径找到RequestDispatcher
     * @param path
     * @return
     */
    @Override
    public RequestDispatcher getRequestDispatcher(String path) {
        if (path == null) {
            return null;
        }
        if (!path.startsWith("/")){
            throw new IllegalArgumentException
                    (sm.getString("applicationContext.requestDispatcher.iae", path));
        }
        String contextPath = context.getPath();
        HttpRequest request = new MappingRequest(contextPath, contextPath + path);
        Wrapper wrapper = (Wrapper) context.map(request, true);
        if(wrapper == null){
            return null;
        }
        return new ApplicationDispatcher(wrapper);
    }

    @Override
    public RequestDispatcher getNamedDispatcher(String s) {
        return null;
    }

    @Override
    public Servlet getServlet(String s) throws ServletException {
        return null;
    }

    @Override
    public Enumeration getServlets() {
        return null;
    }

    @Override
    public Enumeration getServletNames() {
        return null;
    }

    @Override
    public void log(String s) {

    }

    @Override
    public void log(Exception e, String s) {

    }

    @Override
    public void log(String s, Throwable throwable) {

    }

    @Override
    public String getRealPath(String s) {
        return null;
    }

    @Override
    public String getServerInfo() {
        return null;
    }

    @Override
    public String getInitParameter(String s) {
        return null;
    }

    @Override
    public Enumeration getInitParameterNames() {
        return null;
    }

    @Override
    public Object getAttribute(String s) {
        return null;
    }

    @Override
    public Enumeration getAttributeNames() {
        return null;
    }

    @Override
    public void setAttribute(String s, Object o) {

    }

    @Override
    public void removeAttribute(String s) {

    }

    @Override
    public String getServletContextName() {
        return null;
    }


    public ServletContext getFacade() {
        return (this.facade);
    }
}
